<template>
  <div class="cart-sidebar">
    <!-- 购物车为空 -->
    <div v-if="isCartEmpty" class="empty-cart">
      <ShoppingCartOutlined class="empty-icon" />
      <p>购物车是空的</p>
      <a-button type="primary" @click="goToProducts">
        去购物
      </a-button>
    </div>
    
    <!-- 购物车有商品 -->
    <div v-else class="cart-content">
      <!-- 商品列表 -->
      <div class="cart-items">
        <div 
          v-for="item in cartItems" 
          :key="item.id"
          class="cart-item"
        >
          <!-- 商品图片 -->
          <div class="item-image">
            <img :src="item.image" :alt="item.name" />
          </div>
          
          <!-- 商品信息 -->
          <div class="item-info">
            <h4 class="item-name">{{ item.name }}</h4>
            <div class="item-price">¥{{ item.price }}</div>
            
            <!-- 数量控制 -->
            <div class="quantity-control">
              <a-button 
                size="small" 
                @click="decreaseQuantity(item.id)"
                :disabled="item.quantity <= 1"
              >
                <MinusOutlined />
              </a-button>
              <span class="quantity">{{ item.quantity }}</span>
              <a-button 
                size="small" 
                @click="increaseQuantity(item.id)"
                :disabled="item.quantity >= item.stock"
              >
                <PlusOutlined />
              </a-button>
            </div>
          </div>
          
          <!-- 删除按钮 -->
          <div class="item-actions">
            <a-button 
              type="text" 
              size="small" 
              @click="removeFromCart(item.id)"
              danger
            >
              <DeleteOutlined />
            </a-button>
          </div>
        </div>
      </div>
      
      <!-- 购物车底部 -->
      <div class="cart-footer">
        <!-- 总计 -->
        <div class="cart-total">
          <span>总计：</span>
          <span class="total-price">¥{{ cartTotal }}</span>
        </div>
        
        <!-- 操作按钮 -->
        <div class="cart-actions">
          <a-button @click="clearCart" size="large">
            清空购物车
          </a-button>
          <a-button 
            type="primary" 
            size="large" 
            @click="goToCheckout"
            :disabled="isCartEmpty"
          >
            去结算
          </a-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { message, Modal } from 'ant-design-vue'
import {
  ShoppingCartOutlined,
  MinusOutlined,
  PlusOutlined,
  DeleteOutlined
} from '@ant-design/icons-vue'

// 使用Vuex store和Vue Router
const store = useStore()
const router = useRouter()

// 计算属性
const cartItems = computed(() => store.getters['cart/cartItems'])
const cartTotal = computed(() => store.getters['cart/cartTotal'])
const isCartEmpty = computed(() => store.getters['cart/isCartEmpty'])

// 方法
const increaseQuantity = (productId) => {
  const item = cartItems.value.find(item => item.id === productId)
  if (item && item.quantity < item.stock) {
    store.dispatch('cart/updateQuantity', {
      productId,
      quantity: item.quantity + 1
    })
  }
}

const decreaseQuantity = (productId) => {
  const item = cartItems.value.find(item => item.id === productId)
  if (item && item.quantity > 1) {
    store.dispatch('cart/updateQuantity', {
      productId,
      quantity: item.quantity - 1
    })
  }
}

const removeFromCart = (productId) => {
  Modal.confirm({
    title: '确认删除',
    content: '确定要从购物车中删除这个商品吗？',
    onOk: () => {
      store.dispatch('cart/removeFromCart', productId)
      message.success('已从购物车中删除')
    }
  })
}

const clearCart = () => {
  Modal.confirm({
    title: '确认清空',
    content: '确定要清空购物车吗？',
    onOk: () => {
      store.dispatch('cart/clearCart')
      message.success('购物车已清空')
    }
  })
}

const goToProducts = () => {
  router.push('/products')
  store.dispatch('cart/setCartOpen', false)
}

const goToCheckout = () => {
  router.push('/checkout')
  store.dispatch('cart/setCartOpen', false)
}
</script>

<style scoped>
.cart-sidebar {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.empty-cart {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 40px 20px;
  text-align: center;
}

.empty-icon {
  font-size: 64px;
  color: #d9d9d9;
  margin-bottom: 20px;
}

.empty-cart p {
  color: #666;
  margin-bottom: 20px;
  font-size: 16px;
}

.cart-content {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.cart-items {
  flex: 1;
  overflow-y: auto;
  padding: 0 10px;
}

.cart-item {
  display: flex;
  align-items: center;
  padding: 15px 0;
  border-bottom: 1px solid #f0f0f0;
}

.cart-item:last-child {
  border-bottom: none;
}

.item-image {
  width: 60px;
  height: 60px;
  margin-right: 15px;
  flex-shrink: 0;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.item-info {
  flex: 1;
  min-width: 0;
}

.item-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
  margin: 0 0 5px 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-price {
  font-size: 16px;
  font-weight: bold;
  color: #ff4d4f;
  margin-bottom: 8px;
}

.quantity-control {
  display: flex;
  align-items: center;
  gap: 8px;
}

.quantity {
  min-width: 30px;
  text-align: center;
  font-size: 14px;
  color: #333;
}

.item-actions {
  margin-left: 10px;
}

.cart-footer {
  border-top: 1px solid #f0f0f0;
  padding: 20px 10px;
  background-color: #fafafa;
}

.cart-total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  font-size: 16px;
  font-weight: 500;
}

.total-price {
  font-size: 20px;
  font-weight: bold;
  color: #ff4d4f;
}

.cart-actions {
  display: flex;
  gap: 10px;
}

.cart-actions .ant-btn {
  flex: 1;
}

/* 滚动条样式 */
.cart-items::-webkit-scrollbar {
  width: 4px;
}

.cart-items::-webkit-scrollbar-track {
  background: #f1f1f1;
}

.cart-items::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 2px;
}

.cart-items::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .cart-item {
    padding: 12px 0;
  }
  
  .item-image {
    width: 50px;
    height: 50px;
    margin-right: 12px;
  }
  
  .item-name {
    font-size: 13px;
  }
  
  .item-price {
    font-size: 14px;
  }
  
  .cart-actions {
    flex-direction: column;
  }
}
</style> 